﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EZCRS.HCMS.Entity;
using EZCRS.HCMS.Bll;
using EZCRS.HCMS.Bll.baseinfo;

namespace EZCRS.HCMS.Web.manage.hotel
{
    public partial class salepolicy : ManageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (base.BasHotelId == 0)
                Response.Write(CFun.AlertMeg(Resources.Site.MustHotel, "back", ""));

            if (!Page.IsPostBack)
            {
                CheckModule(64);

                Titlepage1.initlabnew = "<a href=\"salepolicy_add.aspx\"><img src=\"../../image/new.gif\" border=\"0\"></a> ";

                //RadioButtonList1.Items.Add(new ListItem("aa","bb"));

                //BindData();

                if (Request.QueryString["action"] != null)
                {
                    //get data
                    if (Request.QueryString["action"].ToLower() == "get")
                    {
                        int iPageIndex = 1;
                        int iPageSize = 15;
                        int iState = 1;

                        if (!string.IsNullOrEmpty(Request.Params["page"]))
                            int.TryParse(Request.Params["page"], out iPageIndex);

                        if (!string.IsNullOrEmpty(Request.Params["rp"]))
                            int.TryParse(Request.Params["rp"], out iPageSize);

                        if (!string.IsNullOrEmpty(Request.Params["query"]))
                            int.TryParse(Request.Params["query"], out iState);


                        Response.Write(GetJSON(iPageIndex, iState, iPageSize));
                        Response.End();
                    }
                }
            }
        }

        /// <summary>
        /// 显示CODE列表
        /// </summary>
        private string GetJSON(int PageIndex, int State, int PageSize)
        {

            BllHotelUtil hotel = new BllHotelUtil();

            DataSet ds = hotel.GetHotelRateList(base.BasPropertyId, State, PageIndex, PageSize);

            int iPageCount = 0;

            if (ds.Tables[1].Rows.Count > 0)
            {
                int.TryParse(ds.Tables[1].Rows[0][0].ToString(), out iPageCount);
            }

            string strJson = "{\"total\":" + iPageCount + ",\"page\":" + PageIndex.ToString() + ",\"rows\":[";

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                strJson += "{\"id\":" + dr["id"].ToString() + ",";
                strJson += "\"cell\":[";

                //strJson += dr["id"].ToString() + ","; //id


                strJson += "\"" + HttpUtility.HtmlEncode(dr["rate"].ToString()) + "\",";
                strJson += "\"" + HttpUtility.HtmlEncode(dr["name"].ToString()) + "\",";

                string strDesc = dr["desc1"].ToString();

                if (strDesc.Length > 80)
                    strDesc = strDesc.Substring(0, 80) + "...";

                strJson += "\"" + HttpUtility.HtmlEncode(strDesc) + "\",";

                bool b = false;

                bool.TryParse(dr["basis"].ToString(), out b);

                if(b)
                    strJson += "\"" + "Y" + "\",";
                else
                    strJson += "\"" + "N" + "\",";

                strJson += "\"" + HttpUtility.HtmlEncode(dr["currency"].ToString()) + "\",";
                strJson += "\"" + dr["status"].ToString() + "\"";

                strJson += "]},";
            }

            strJson = strJson.Replace("\r\n", " ");
            strJson = strJson.TrimEnd(',');
            strJson += "]}";

            return strJson;
        }
    }
}
